package com.github.ilioili.justdoit.util;

import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.github.ilioili.justdoit.R;

/**
 * Created by hantuo on 16/5/31.
 */
public class AniUtil {

    public static void animate(int duration, final ProgressListener listener) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setDuration(duration);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                listener.onGoing(value);
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                listener.onStart();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                listener.onEnd();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                listener.onEnd();
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                listener.onStart();
            }
        });
        valueAnimator.start();
    }

    public static void animateColor(final int startColor, final int endColor, int duration, final View target){
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setDuration(duration);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            ArgbEvaluator argbEvaluator = new ArgbEvaluator();
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                int curColor = (int) argbEvaluator.evaluate(value, startColor, endColor);
                target.setBackgroundColor(curColor);
            }
        });
        valueAnimator.start();
    }

    public static void dropDownFloatingBtn(final View target){
        target.setEnabled(false);
        Animation animation = AnimationUtils.loadAnimation(target.getContext(), R.anim.drop_to_bottom);
        animation.setDuration(300);
        animation.setFillAfter(true);
        target.startAnimation(animation);
    }
    public static void popupUpFloatingBtn(final View target){
        Animation animation = AnimationUtils.loadAnimation(target.getContext(), R.anim.popup_from_bottom);
        animation.setDuration(300);
        animation.setFillAfter(true);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                target.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                target.setEnabled(true);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        target.startAnimation(animation);
    }


    public interface ProgressListener {
        void onGoing(float value);

        void onStart();

        void onEnd();
    }
}
